package webrtcwrappers;

import android.content.Context;
import android.content.pm.PackageManager;

import androidx.core.content.ContextCompat;

import org.webrtc.Camera1Enumerator;
import org.webrtc.Camera2Enumerator;
import org.webrtc.CameraEnumerationAndroid;
import org.webrtc.CameraEnumerator;
import org.webrtc.CameraVideoCapturer;
import org.webrtc.SurfaceTextureHelper;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoFileRenderer;
import org.webrtc.VideoSource;

import java.util.List;

import utils.WebRTCEglUtils;

public class WebRTCCapturer {

    private final CameraVideoCapturer videoCapturer;
    private VideoSource videoSource;
    private CameraEnumerationAndroid.CaptureFormat format;

    public boolean hasPermission(Context context) {
        return (ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(context, android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED);
    }


    public WebRTCCapturer(CameraVideoCapturer videoCapturer) {
        this.videoCapturer = videoCapturer;
    }

    public void startVideo(Context context) {
        videoCapturer.initialize(SurfaceTextureHelper.create("fancyWebRTCVideoCapturer", WebRTCEglUtils.getRootEglBaseContext()), context, videoSource.getCapturerObserver());
        videoCapturer.startCapture(format.width, format.height, format.framerate.max);
    }

    public void setVideoSource(VideoSource videoSource) {
        this.videoSource = videoSource;
    }

    public void setFormat(CameraEnumerationAndroid.CaptureFormat format) {
        this.format = format;
    }

    public void stopVideo() {
        try {
            videoCapturer.stopCapture();
        } catch (InterruptedException e) {
        }
    }

    public static VideoCapturer createCapturer(Context context) {
        CameraEnumerator enumerator;
        if (Camera2Enumerator.isSupported(context)) {
            enumerator = new Camera2Enumerator(context);
        } else {
            enumerator = new Camera1Enumerator(false);
        }

        String[] deviceNames = enumerator.getDeviceNames();
        for (String deviceName : deviceNames) {
            List<CameraEnumerationAndroid.CaptureFormat> formatList = enumerator.getSupportedFormats(deviceName);
            CameraEnumerationAndroid.CaptureFormat selectedFormat = formatList.get(formatList.size() - 1); // Default to lowest
            for (CameraEnumerationAndroid.CaptureFormat format : enumerator.getSupportedFormats(deviceName)) {
                selectedFormat = format;
                break;
            }

            if (enumerator.isFrontFacing(deviceName)) {
                CameraVideoCapturer capturer = enumerator.createCapturer(deviceName, null);
                if (capturer != null) {
                    WebRTCCapturer fancyWebRTCCapturer = new WebRTCCapturer(capturer);
                    fancyWebRTCCapturer.setFormat(selectedFormat);
                    return capturer;
                }
            }
        }

        for (String deviceName : deviceNames) {
            if (!enumerator.isFrontFacing(deviceName)) {

                List<CameraEnumerationAndroid.CaptureFormat> formatList = enumerator.getSupportedFormats(deviceName);
                CameraEnumerationAndroid.CaptureFormat selectedFormat = formatList.get(formatList.size() - 1); // Default to lowest
                for (CameraEnumerationAndroid.CaptureFormat format : enumerator.getSupportedFormats(deviceName)) {
                    selectedFormat = format;
                    break;
                }

                CameraVideoCapturer capturer = enumerator.createCapturer(deviceName, null);
                if (capturer != null) {
                    WebRTCCapturer fancyWebRTCCapturer = new WebRTCCapturer(capturer);
                    fancyWebRTCCapturer.setFormat(selectedFormat);
                    return capturer;
                }
            }
        }


        return null;
    }

}
